package belajarzk.action.maintenance;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.zkoss.zk.ui.sys.ExecutionsCtrl;
import org.zkoss.zul.Comboitem;
import org.zkoss.zul.Grid;
import org.zkoss.zul.ListModel;
import org.zkoss.zul.SimpleListModel;
import org.zkoss.zul.Textbox;
import org.zkoss.zul.Window;

import belajarzk.database.hibernate.HibernateUtil;
import belajarzk.database.model.Tbmrole;
import belajarzk.ui.render.combo.ComboTbmroleRenderer;
import belajarzk.ui.render.grid.TbmroleRenderer;


public class TbmroleAction {

	@SuppressWarnings("unchecked")
	public synchronized static void load(org.zkoss.zul.Combobox combobox) {
		Session session = HibernateUtil.currentSession();

		List<Tbmrole> jobs = session.createCriteria(Tbmrole.class)
				.setMaxResults(belajarzk.common.Common.MAX_RESULT).list();

		combobox.setItemRenderer(new ComboTbmroleRenderer());
		ListModel strset = new SimpleListModel(jobs);
		combobox.setModel(strset);
		
		
	}

	public synchronized static void loadAdmin(org.zkoss.zul.Combobox combobox) {
		Comboitem comboitem = new Comboitem();
		comboitem.setLabel("Admin");
		comboitem.setValue("1");
		combobox.appendChild(comboitem);
		comboitem = new Comboitem();
		comboitem.setLabel("User");
		comboitem.setValue("0");
		combobox.appendChild(comboitem);
	}

	public synchronized static void resetInput(Tbmrole job) {
		Window window = ((Window) ExecutionsCtrl.getCurrentCtrl()
				.getCurrentPage().getVariable("addWindow"));
		((Textbox) window.getVariable("nama", true)).setValue(job == null ? ""
				: job.getRoleName());
		((Textbox) window.getVariable("idTbmrole", true))
				.setValue(job == null ? "" : job.getRoleId() + "");
		// Combobox combobox = ((Combobox) window.getVariable("admin", true));
		// combobox.setSelectedIndex(job == null ? -1 :
		// (job.getAdmin().trim().equals("1") ? 0 : 1));
	}

	public synchronized static void isPrepareEdit(Boolean edit) {
		Window window = ((Window) ExecutionsCtrl.getCurrentCtrl()
				.getCurrentPage().getVariable("addWindow"));
		((Textbox) window.getVariable("nama", true)).setReadonly(false);
	}

	public Boolean isExist(String jobId) {
		Session session = HibernateUtil.currentSession();

		Integer count = ((Number) session.createCriteria(Tbmrole.class)
				.setProjection(Projections.count("roleId")).add(
						Restrictions.idEq(jobId)).uniqueResult()).intValue();

		// System.out.println("count = " + count);

		
		
		return count.equals(1);
	}

	public void save(Tbmrole job) {
		Session session = HibernateUtil.currentSession();

		if (isExist(job.getRoleId())) {

			String name = job.getRoleName();
			// String admin = job.getAdmin();
			job = (Tbmrole) session.load(Tbmrole.class, job.getRoleId());
			job.setRoleName(name);
			// job.setAdmin(admin);

			session.update(job);

		} else {

			session.save(job);

		}

		
		

		searchDefault();
		resetInput(null);

	}

	public void searchDefault() {
		Textbox srcnama = ((Textbox) ExecutionsCtrl.getCurrentCtrl()
				.getCurrentPage().getVariable("srcnama"));
		Tbmrole jobLike = new Tbmrole();
		jobLike.setRoleName(srcnama.getValue());
		search(jobLike);
	}

	@SuppressWarnings("unchecked")
	public void search(Tbmrole jobLike) {

		Session session = HibernateUtil.currentSession();

		List<Tbmrole> job = session.createCriteria(Tbmrole.class).addOrder(
				Order.desc("id")).add(
				Restrictions.ilike("roleName",
						jobLike.getRoleName() == null ? "" : jobLike
								.getRoleName(), MatchMode.ANYWHERE))
				.setMaxResults(belajarzk.common.Common.MAX_RESULT).list();

		ListModel strset = new SimpleListModel(job);

		Grid grid = (Grid) ExecutionsCtrl.getCurrentCtrl().getCurrentPage()
				.getVariable("grid");
		grid.setRowRenderer(new TbmroleRenderer());
		grid.setModel(strset);
		grid.setFixedLayout(true);
		grid.renderAll();
		

		
		
	}

}
